<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button class="bubbleSort">冒泡排序</button><br>
    <button class="quickSrot">快速排序</button><br>
    <button class="binarySearch">二分查找</button>

    <script>
        // 冒泡排序
        let arr = [5,6,9,8,7,2,3,1,0,4];
        let bubbleButton = document.querySelector('.bubbleSort');
        bubbleButton.onclick = function(){
            console.log('排序前：');
            Show();
            for(let i = 0; i < arr.length; i++){
                let exchange = false;
                for(let j = 1; j < arr.length - i; j++){
                    if(arr[j-1] > arr[j]){
                        Swap(j-1, j);
                        exchange = true;
                    }
                }
                if(exchange == false){
                    break;
                }
            }
            console.log('排序后：');
            Show();
        }

        function Swap(i, j){
            let tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
        function Show(){
            let resultString = '';
            for(let i=0; i<arr.length; i++){
                resultString += arr[i] + ' ';
            }
            console.log(resultString);
        }


        // 快速排序
        let quickButton = document.querySelector('.quickSrot');
        quickButton.onclick = function(){
            console.log('排序前：');
            Show();
            quickSort(arr, 0, arr.length-1);
            console.log('排序后：');
            Show();
        }
        function quickSort(arr, left, right){
            if(left >= right){
                return;
            }
            let mid = quickSortFunc1(arr, left, right);
            quickSort(arr, left, mid-1);
            quickSort(arr, mid+1, right);
        }
        function quickSortFunc1(arr, left, right){
            let begin = left;
            let end = right;
            let pivot = begin;
            let key = arr[begin];
            while(begin < end){
                while(begin < end && key <= arr[end]){
                    end--;
                }
                arr[pivot] = arr[end];
                pivot = end;
                while(begin < end && key >= arr[begin]){
                    begin++;
                }
                arr[pivot] = arr[begin];
                pivot = begin;
            }
            arr[begin] = key;
            return pivot;
        }

        // 二分查找
        let binarySearch = document.querySelector('.binarySearch');
        binarySearch.onclick = function(){
            let searchresult = binarySearchFunc(5);
            console.log('查找结果：' + searchresult);
        }
        function binarySearchFunc(x){
            let begin = 0, end = arr.length-1;
            while(begin < end){
                let mid = parseInt((begin+end)/2);
                if(x > arr[mid]){
                    begin = mid+1;
                }else if(x < arr[mid]){
                    end = mid-1;
                }else{
                    return mid;
                }
            }
            return -1;
        }


    </script>
</body>
</html>